import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { ApiService } from '@core/services/http/api.service';
import { ApesModalRef } from '@shared/apes/modal';
import { deepClone } from '../../utils/utils';
import { UserAuthService } from '@core/services/user-auth.service';
import { FormlyFieldConfig } from '@ngx-formly/core';
import { AllModules, Module } from '@ag-grid-enterprise/all-modules';
import { GridConfigService } from '@core/config/grid-config.service';
import { toBoolean } from '@shared/apes/core';
import { ApesContextMenuService } from '@shared/ag-grid-ext/context-menu';


@Component({
  selector: 'dialog-carControl',
  templateUrl: './dialog-carControl.component.html',
})
export class DialogCarControlComponent implements OnInit {
  model: any = {};
  options: any;
  serData: any = {};

  @Input() get data() {
    return this.serData;
  }

  set data(value: object) {
    if (value) {
      this.serData = value;
      this.getData();
    }

  }

  @Output() closeDialog = new EventEmitter<any>();
  showHideBtn: boolean = false;

  carInfo: any = {};
  carFeeDetails: any = {};
  carSolutionInformation: any[] = [];
  isLoading: boolean = false;
  carColumnDefs: any[] = [
    { headerName: '商品', field: 'PRODUCT', width: 280 },
    { headerName: '数量', field: 'SALE_QTY', width: 70 },
    { headerName: '金额', field: 'PFT_PRICE_UNIT', width: 70 },
    { headerName: '状态', field: 'WORK_STATE', width: 90 },
    { headerName: '要货数量', field: 'STOCK_STATE', width: 120 }, // 要货状态
    { headerName: '开始时间', field: 'WORK_STATE_DATE', width: 150 },
  ];

  /** grid 相关 **/
  private gridApi;
  private gridColumnApi;
  frameworkComponents: any;
  modules: Module[] = AllModules;
  localeText: any;


  constructor(private apiService: ApiService,
              protected userService: UserAuthService,
              protected gridConfigService: GridConfigService,
              protected contextService: ApesContextMenuService,
              public modal: ApesModalRef) {
    this.localeText = gridConfigService.getLocaleText();
  }

  ngOnInit() {
    this.getData();
  }

  getData() {
    this.initData();
    this.getCarInfo();
    this.getCarCurrentConsume();
  }

  initData() {
    this.carInfo = {};
    this.carFeeDetails = [];
    this.carSolutionInformation = [];
  }

  getCarInfo() {
    let api = 'fn.WorkDeskVehicleControlService.vehicleControlCarInfo';
    let data = deepClone(this.data);
    let platform = this.userService.getUserInfoObj() ? this.userService.getUserInfoObj().platform : '';
    const setData = { ...data, platform: platform };
    this.apiService.post(api, setData).subscribe(
      (body) => {
        console.log(body);
        if (body) {
          if (body) {
            this.carInfo = body.carInfo;
          }
        }
      });
  }

  getCarCurrentConsume() {
    this.isLoading = true;
    let api = 'fn.WorkDeskVehicleControlService.vehicleControlCurrentConsume';
    let data = deepClone(this.data);
    let platform = this.userService.getUserInfoObj() ? this.userService.getUserInfoObj().platform : '';
    const setData = { ...data, platform: platform };
    this.apiService.post(api, setData).subscribe(
      (body) => {
        this.isLoading = false;
        if (body) {
          console.log(body);
          if (body) {
            this.carFeeDetails = body.amountDetail;
            this.carSolutionInformation = body.currentConsume;
          }
        }
      });
  }

  showHidden(value) {
    let show = '';
    let replaceValue = '$1****$2';
    let hiddenRegExp = /^(.{3})(?:.{4})(.{4})$/;
    if (this.showHideBtn) {
      show = value;
    } else {
      show = value.replace(hiddenRegExp, replaceValue);
    }
    return show;
  }

  filterValue(value) {
    return value != null ? value : '--';
  }

  onGridReady(params) {
    this.gridApi = params.api;
    this.gridColumnApi = params.columnApi;
  }

  closeDialogBtn() {
    this.closeDialog.emit(true);
  }

  openToBill(param) {
    let resultData = param;
    resultData['LICENSE_NUMBER'] = param['CAR_LICENSE']
    let api = 'fn.workDeskConfig.workDeskByDKD';
    let formId = 'form/crm.servicePlanItem';
    let config = { 'OAInterface': false, 'api': api };
    this.contextService.openContextLink(resultData, formId, config);
  }

}
